/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package exp.algorithm.sic.scalerf;


import exp.algorithm.sic.GaussianArray;
import exp.algorithm.sic.TimeSeries;
import timeseriesweka.classifiers.TSF;
import weka.classifiers.Classifier;
import weka.core.Capabilities;
import weka.core.Instance;
import weka.core.Instances;

/**
 * 类Octave.java的实现描述：表示8度金字塔中的一个8度空间，即以尺寸为坐标的某一尺寸上的那个8度空间
 * 
 * @author axman 2013-6-27 上午11:30:08
 */
public class OctaveSpace implements Classifier{
	
	OctaveSpace down; // down指的是下一个8度空间
	OctaveSpace up;
	TimeSeries baseImg; // 当前8度空间的原始图片，由上一个8度空间的某层（默认为倒数第三层）获取
	public float baseScale; // 原始图片在塔中的原始尺度
	public int indexInPyramid;
	public Instances osInst; // 同一尺寸用不同模糊因子模糊后的高斯图像集
	GaussianArray gauss ;
	TSF tsf = new TSF();
	float kernel = -1;
	boolean blur = true;
	public OctaveSpace(float kernel) {
		this.kernel = kernel;
		if(kernel>0){
			gauss = new GaussianArray(kernel);
		}
	}
	
	public String toString(){
		return super.toString();
	}
	
	public Instances getLastGaussianImg() {
		return osInst;
	}

	public Instances getInstances(){
		return osInst;
	}
	

	public void clear() {
	}

	Instances blurInstances(Instances insts){
		if(gauss == null) return insts;
		for(int i =0;i<insts.numInstances();i++){
			insts.set(i, blurInstance(insts.instance(i)));
		}
		return insts;
	}
	Instance blurInstance(Instance inst){
		if(gauss == null) return inst;
		TimeSeries ts = TimeSeries.fromInstance(inst);
		TimeSeries res = gauss.convolve(ts);
		for(int i=0;i<res.length();i++){
			inst.setValue(i, res.data[i]);
		}
		return inst;
	}
	
	@Override
	public void buildClassifier(Instances data)  {
		this.osInst = data;
		try {
			if(blur){
				tsf.buildClassifier(blurInstances(data));
			}else{
				tsf.buildClassifier(osInst);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	public double classifyInstance(Instance instance) {
		try {
			if(blur){
				return tsf.classifyInstance(blurInstance(instance));
			}else
				return tsf.classifyInstance(instance);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}


	@Override
	public double[] distributionForInstance(Instance instance)  {
		try {
			if(blur){
				return tsf.distributionForInstance(instance);
			}else
				return tsf.distributionForInstance(instance);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	@Override
	public Capabilities getCapabilities() {
		return null;
	}



}
